<?php
declare (strict_types=1);

namespace app\common\model;

use addons\Target\facade\Target;
use think\facade\Cache;

class Platoon extends Base
{
    protected $name = 'platoon';

    protected function user(): object
    {
        return $this->hasOne(User::class, 'id', 'master_uid');
    }

    /**
     * @param $user
     * @param $order
     * @param $level
     * @return array
     */
    public static function _add($user, $order, $level=1): array
    {
        try {
            $system = Cache::get('system');
            $last = parent::where('level', $level)->order('id', 'desc')->find();
            $lastId = $last ? $last->id : 0;
            $field = 'push_platoon'.$level;
            $share_num = $user->$field;
            $bonus_amount = 0;
            if($level == 1){
                $bonus_amount = $system['platoon_amount1'];
            }elseif ($level == 2){
                $bonus_amount = $system['platoon_amount2'];
            }elseif ($level == 3){
                $bonus_amount = $system['platoon_amount3'];
            }
            $new = parent::create([
                'uid' => $user->id,
                'parent_uid' => $user->parent_id,
                'master_uid' => $user->master_id,
                'front_id' => $lastId,
                'level' => $level,
                'is_sub' => $user->id == $user->master_id ? 0 : 1,
                'share_num' => $share_num,
                'bonus_amount' => $bonus_amount,
                'up_bonus' => $lastId ? 1 : 0
            ]);
            User::where('id', $user->master_id)->save(['is_queue'=>1]);
            /*if($level == 1){
                if($pUser = User::find($user->parent_id)){
                    if($pUser->platoon_id == 2 && !$pUser->is_queue){
                        $pUser->platoon2_push_vip ++;
                        if($pUser->platoon2_push_vip >= Cache::get('system')['platoon2_zt_vip']){
                            if($pUser->assets->contribute < 15) {
                                Target::Account()->basics($pUser->id, 'contribute', $system['platoon_zt_contribute2'], "经理邀请VIP进公排得贡献值", '+')->createNormal();
                            }
                            $sub = OrderTemporary::getSubUser($pUser, $pUser->platoon_id);
                            Platoon::_add($sub, $order, $pUser->platoon_id);
                            $pUser->is_queue = 1;
                            $pUser->platoon2_push_vip = 0;
                        }
                        $pUser->save();
                    }elseif ($pUser->platoon_id == 3 && !$pUser->is_queue){
                        $pUser->platoon3_push_vip ++;
                        if($pUser->platoon3_push_vip >= Cache::get('system')['platoon3_zt_vip']){
                            if($pUser->assets->contribute < 25) {
                                Target::Account()->basics($pUser->id, 'contribute', $system['platoon_zt_contribute3'], "店长邀请VIP进公排得贡献值", '+')->createNormal();
                            }
                            $sub = OrderTemporary::getSubUser($pUser, $pUser->platoon_id);
                            Platoon::_add($sub, $order, $pUser->platoon_id);
                            $pUser->is_queue = 1;
                            $pUser->platoon3_push_vip = 0;
                        }
                        $pUser->save();
                    }
                }
            }*/

            if($last){
                $title = '';
                if($level == 1){
                    $title = 'VIP';
                }elseif ($level == 2){
                    $title = '经理';
                }elseif ($level == 3){
                    $title = '店长';
                }

                if($last->up_bonus == 1 && $new->up_bonus == 1) {
                    $top = parent::where(['status'=>1, 'level'=>$level])->order('id', 'asc')->find();
                    if($top){
                        Account::bonusEntry($top->master_uid, $bonus_amount, $title.'分红', $user->master_id, $level, $order->id);
                        $msg = "会员【ID：{$top->master_uid}】获得{$title}分红【ID：{$last->master_uid}和ID：{$new->master_uid}满足2出1】";
                        Logs::adds($msg);
                        $top->status = 0;//出局
                        $top->save();

                        if($level == 2){
                            User::where('id', $top->master_uid)->save(['is_queue'=>0, 'platoon2_push_vip'=>0]);
                        }elseif($level == 3){
                            User::where('id', $top->master_uid)->save(['is_queue'=>0, 'platoon3_push_vip'=>0]);
                        }

                        $last->up_bonus = 0;
                        $last->save();

                        $new->up_bonus = 0;
                        $new->save();
                    }
                }
            }

            return ['code' => 1, 'msg' => 'success'];
        } catch (\Exception $e) {
            Logs::exception($e);
            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function platoon($level, $platoon, $user, $order)
    {
        $system = Cache::get('system');
        $amount = $maxAmount = 0;
        $intro = '';
        if($level == 1){
            $amount = $system['platoon_amount1'];//1500;
            $maxAmount = $system['platoon_max_amount1'];//7500;
            $intro = 'VIP';
        }elseif($level == 2){
            $amount = $system['platoon_amount2'];//7500;
            $maxAmount = $system['platoon_max_amount2'];//75000;
            $intro = '经理';
        }elseif($level == 3){
            $amount = $system['platoon_amount3'];//37500;
            $maxAmount = $system['platoon_max_amount3'];//375000;
            $intro = '店长';
        }

        $list = parent::where(['status'=>1, 'level'=>$level])->select();
        foreach ($list as $item){
            $bonusFlag = false;
            $msg = '';
            if($platoon->parent_uid == $item->master_uid){//邀请
                $item->share_num ++;
                $item->save();
                if($item->share_num < 2){
                    continue;
                }
                if($item->share_num == 2 || $item->share_num % 2 == 0){
                    $bonusFlag = true;
                    $msg = "会员【ID：{$item->master_uid}】推荐【ID：{$platoon->master_uid}】，人数达到{$item->share_num}，获得{$amount}";
                }
            }else{//滑落
                if($item->share_num > 2 && $item->share_num >= $item->next_share_num && $item->share_num % 2 == 1){
                    $bonusFlag = true;
                    $msg = "会员【ID：{$item->master_uid}】的推荐人数为{$item->share_num}，会员【ID：{$platoon->master_uid}】进入{$intro}公排，获得{$amount}";
                }
            }
            if($bonusFlag){
                $item->next_share_num = $item->share_num + 1;
                $item->bonus_amount += $amount;
                if($item->bonus_amount >= $maxAmount){
                    $item->status = 0;
                    User::where('id', $item->master_uid)->save(['out_'.$level=>1]);
                }
                $item->save();
                Account::bonusEntry($item->master_uid, $amount, $intro.'分红', $user->id, $level, $order->id);
                Logs::adds($msg);
            }
        }
        return true;
    }


}